package com.caterina.components;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.caterina.resources.CColors;
import com.caterina.resources.CWords;

public abstract class CButton extends JPanel{
	public CButton(String text){
		this(CWords.getWord(text),false);
	}
	public CButton(String text,boolean asLink){
		super();
		this.asLink=asLink;
		if(!asLink){
			this.setBackground(CColors.BUTTON_NORMAL_CATERINA);
			this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			this.setForeground(CColors.WHITE);
		}else{
			this.setForeground(CColors.black);
			this.normalText=CWords.getWord(text);
			this.underlinedText="<html><u>"+CWords.getWord(text)+"</u></html>";
		}
		this.setLayout(new BorderLayout());
		this.addText(CWords.getWord(text));
		this.addListeners();
		this.setFocusable(true);
	}
	private void addText(String textToWrite){
		textPanel=new JPanel();
		textPanel.setOpaque(false);
		text=new JLabel(textToWrite);
		text.setAlignmentX(CENTER_ALIGNMENT);
		if(!asLink){
			text.setFont(new Font("Tahoma",Font.PLAIN,16));
			text.setForeground(CColors.white);
		}else{
			this.setBackground(CColors.PANEL_BACKGROUND);
			text.setFont(new Font("Tahoma",Font.ROMAN_BASELINE,13));
			text.setForeground(CColors.LABEL_PLANE_FOREGROUND);
			this.setAlignmentY(CENTER_ALIGNMENT);
			this.setAlignmentX(LEFT_ALIGNMENT);
		}
		textPanel.add(text);
		this.add(textPanel,BorderLayout.SOUTH);
	}
	private void addListeners(){
		this.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent arg0) {if (arg0.getKeyChar()=='\n')CButton.this.action();}
			public void keyReleased(KeyEvent arg0) {}
			public void keyPressed(KeyEvent arg0) {if (arg0.getKeyChar()=='\n')CButton.this.action();}
		});
		this.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent arg0) {CButton.this.normal();}
			public void focusGained(FocusEvent arg0) {CButton.this.focus();}
		});
		this.addMouseListener(new MouseListener() {		
			public void mouseReleased(MouseEvent arg0) {CButton.this.normal();}	
			public void mousePressed(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {CButton.this.normal();}
			public void mouseEntered(MouseEvent arg0) {CButton.this.focus();}
			public void mouseClicked(MouseEvent arg0) {CButton.this.action();}
		});
	}
	private void focus(){
		if(!asLink){
			this.setBackground(CColors.BUTTON_FOCUSED_CATERINA);
		}else{
			text.setForeground(linkForeFocused);
			text.setText(underlinedText);
		}
		this.onFocus();
	}
	private void normal(){
		if(!asLink){
			this.setBackground(CColors.BUTTON_NORMAL_CATERINA);
		}else{
			text.setForeground(linkForeNormal);
			text.setText(normalText);
		}
	}
	private void action(){
		if (!asLink){
			this.setBackground(CColors.BUTTON_PRESSED_CATERINA);
		}else{
			text.setForeground(linkForePressed);
			text.setText(normalText);
		}
		this.onAction();
	}
	public void setText(String text){
		this.text.setText(CWords.getWord(text));
	}
	public String getText(){
		return this.text.getText();
	}
	public void setLinkForegrounds(Color normal,Color focused,Color pressed){
		this.linkForeNormal=normal;
		this.linkForePressed=pressed;
		this.linkForeFocused=focused;
		this.normal();
	}
	public void setLeftMode(boolean mode){
		if (mode==true)this.text.setAlignmentX(JLabel.LEFT_ALIGNMENT);
		else this.text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
	}
	private String normalText,underlinedText;
	private Color linkForeNormal=CColors.LABEL_FOREGROUND;
	private Color linkForePressed=CColors.BUTTON_PRESSED_CATERINA;
	private Color linkForeFocused=CColors.BUTTON_FOCUSED_CATERINA;
	public abstract void onAction();
	public abstract void onFocus();
	private JPanel textPanel;
	private boolean asLink;
	private JLabel text;
	private static final long serialVersionUID = 1L;
}
